package cn.bgs.service.impl;

import cn.bgs.common.MD5;
import cn.bgs.common.R;

import cn.bgs.common.GuliException;
import cn.bgs.common.TokenUtil;
import cn.bgs.feign.UserFeign;
import cn.bgs.mapper.UserMapper;
import cn.bgs.pojo.User;
import cn.bgs.service.IUserService;
import cn.bgs.vo.Residents;
import cn.bgs.vo.UserFile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户/管理员表 服务实现类
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper mapper;
    //新建报修记录查询用户列表
    @Override
    public List<User> intoFind(String relationShip) {
        return mapper.intoFind(relationShip);
    }

    Logger log= LogManager.getLogger();

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserFeign userFeign;


    @Override
    public User getUser(String userId) {
        User user = userMapper.getUser(userId);
        return user;
    }

    @Override
    public R whlogin(String email,String password) {

        System.out.println(email+"================="+password);

        User user = userMapper.login(email);

        if (user!=null){
            if (user.getPassWord().equals(password)){
                return R.ok().data("user", user);
            }else {
                return R.error().data("error", "用户名或密码错误");
            }
        }else {
            return R.error().data("error", "用户不存在");
        }
    }

    @Override
    public R register(String email, String password) {

        User user = userMapper.login(email);

        if (user==null){
            Integer i = userMapper.register(email,password);
            if (i!=0){
                return R.ok();
            }else {
                return R.error().data("error", "注册失败");
            }
        }else {
            return R.error().data("error", "用户已存在");
        }
    }

    @Override
    public R forgot(String email, String password) {

        User user = userMapper.login(email);

        if (user!=null){
            password = MD5.encrypt(password);
            Integer i = userMapper.updatePassword(user.getUserId(),password);
            if (i!=0){
                return R.ok();
            }else {
                return R.error().data("error", "修改失败");
            }
        }else {
            return R.error().data("error", "用户不存在");
        }

    }




    @Override
    public User login(String phone, String password) {
        User user = getUser(phone,password);
        return user;
//        if (user!=null){
//            String token = TokenUtil.getToken(user.getEmail(),user.getPassWord());
//            user.setToken(token);
//
//        }else {
//            throw new GuliException(20001,"无此用户");
//        }

    }

    @Override
    public User getPhone(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.eq("phone",phone);
        User user = baseMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public int selectEmail(String email) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.eq("email",email);
        User user = baseMapper.selectOne(wrapper);
        if (user==null){
            return 0;
        }else {
            return 1;
        }

    }

    @Override
    public void registerInfo(User user) {
        LocalDateTime localDateTime = LocalDateTime.now();
        user.setUserNumber("zhsq123");
        if (user.getSex()=="0"){
            user.setSex("男");
        }else {
            user.setSex("女");
        }
        user.setRelationShip("本人");
        user.setIdentity("业主");
        user.setRegistrationTime(localDateTime);
        user.setResidentSource("app");
        user.setCreateTime(localDateTime);
        user.setUpdateTime(localDateTime);
        user.setDeleteFlag("n");
        save(user);
    }

    @Override
    public void updatePwd(User user) {
        LocalDateTime localDateTime = LocalDateTime.now();
        user.setUpdateTime(localDateTime);
        userMapper.updatePwd(user);
    }

    @Override
    public void appUserInfo(UserFile userFile) {

        LocalDateTime localDateTime = LocalDateTime.now();
        userFile.setUserNumber("zhsq666");
        if (userFile.getSex()=="0"){
            userFile.setSex("男");
        }else {
            userFile.setSex("女");
        }
        userFile.setIdentity("业主");
        userFile.setRegistrationTime(localDateTime);
        userFile.setResidentSource("app");
        userFile.setCreateTime(localDateTime);
        userFile.setUpdateTime(localDateTime);
        userFile.setDeleteFlag("n");

        userMapper.appUserInfo(userFile);

        userMapper.appFileAdd(userFile);

        userFeign.addHouseUser(userFile.getUserId());



    }



    @Override
    public R findResidents(String type, String name) {

        List<Residents> residentsList = userMapper.findResidents(type,name);

        return R.ok().data("residentsList", residentsList);
    }

    @Override
    public R delResidents(Integer id) {

        Integer i = userMapper.delResidents(id);

        Integer l = userMapper.delcellIntermediate(id);

        if(i!=0&&l!=0){
            return R.ok();
        }else {
            return R.error().data("error", "删除失败");
        }

    }

    @Override
    public R countWorkbench() {

        Integer i = userMapper.countResidents();

        return R.ok().data("countResidents",i);
    }

    @Override
    public R updateResident(Residents residents) {

        Integer i = userMapper.updateResident(residents);

        Integer l = userMapper.updateFile(residents.getUserId(), residents.getType(),residents.getFileUrl());

        if (i!=0&&l!=0){
            return R.ok();
        }else {
            return R.error().data("error", "编辑失败");
        }

    }

    @Override
    public R auditResident(Integer userId, String audit) {

//        Integer i = userMapper.updateAudit(userId,audit);

        Integer l = userMapper.updateMiddle(userId,audit);

        if (l!=0){
            return R.ok();
        }else {
            return R.error().data("error", "审核失败");
        }
    }


    private User getUser(String phone, String password) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.eq("phone",phone);
        wrapper.eq("pass_word",password);

        User ee=null;
        try {
            ee = baseMapper.selectOne(wrapper);
        }catch (Exception e){
            e.printStackTrace();

        }
        return ee;
    }
}
